#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h>
#include "pic.h"
#include "hello.h"

#define DEBUG
#ifdef DEBUG
#define DebugPrintf(format, arg...)              \
           printf(format, ## arg)

#else

#define DebugPrintf(format, arg...) do {  } while (0)

#endif

int crc16_table[256];

const  adebug_param_table param_table[]={
    {PICTSTREAM, FID_picture,   Ftype_stream}, //图片流传输1
    {DATASTREAM, FID_left_clif, Ftype_char}, //开关机0
    {DATASTREAM, FID_right_clif,Ftype_char}
};


/*发送打包数据函数
    返回 -1 发送失败
    返回 0 发送成功

*/

char send_pack_data(char *source, int lenth,FID_NAME fid_name){
    static int first_frame_number=0;
    static char temp_mux=0;
    int result = -1;
    if (temp_mux == 1)
    {
       return result;
    }
    temp_mux=1;
    
   // int max = sizeof(param_table) / sizeof(adebug_param_table);
    
    size_t i;
    for ( i = 0; i < sizeof(param_table) / sizeof(adebug_param_table); i++)
    {
        /* code */
        if ( fid_name == param_table[i].name_fid )
        {
            result = 0;
            
            break;
        }
        
    }

    if (result == 0)
    {
        //
       
        first_frame_number++;
        result=pack_prease_send(source,lenth,first_frame_number,param_table[i].channel,param_table[i].type);
      
    }

    temp_mux=0;
    return result;
}
/*
输入参数：
        source：输入数据的地址，
        lenth：数据长度，
        frame：数据个数，当为图片流时，默认为1，
        channel：传输的数据通道
*/

// int auto_send(char *source ,int lenth,int frame,char channel){
//     DebugPrintf("sendchannel:0x%x\r\n",channel);
//     pack_prease_send(source,lenth,frame,channel);
// }
char pack_prease_send(char *data, int len, int frame_numb, CHANNEL_TYPE channel, FID_TYPE type){
    
    char result_tmp = -1;
   
        switch (channel)
        {
            case 0x1:
                {
                    result_tmp=handle_stream_data(data,len, frame_numb, PICTSTREAM);  
                }
                    break;
            case 0x2:
                {
                    result_tmp=handle_common_data(data, len, frame_numb, DATASTREAM);
                }
                    break;
            case 0x3:
                {
                    result_tmp=handle_stream_data(data, len, frame_numb, FILESTREAM);
                } 
                    break;
            
            default:
                    DebugPrintf("NO channel %d\r\n",channel);
                    break;
            
       
        }
        return result_tmp;
}
char handle_stream_data(char *data, int len,int frame_numb,char channel){
                char result_tmp = -1;
                
                int pack_left=len%VALUE_LEN;
                int pack_number=len/VALUE_LEN;
               
                //frame_numb=1;
                DebugPrintf("need send:%d,%d\r\n",pack_number,pack_left);
                char tempdata[1]={0};
                send_data_usb(tempdata,0,channel,frame_numb,1);
                int i;
                for (i=0;i<pack_number;i++){
                // ui->textBrowser_FileName->setText(pack_number);
                    send_data_usb(data,VALUE_LEN,channel,frame_numb,2);
                    data=data+VALUE_LEN;
                    usleep(1000);//need to do
                }
                if(pack_left!=0){
                    send_data_usb(data,pack_left,channel,frame_numb,2);
                }
                send_data_usb(tempdata,0,channel,frame_numb,3);
                result_tmp = 0;
            return result_tmp;

}

char handle_common_data(char *data, int len,int frame_numb,char channel){
                if (len>VALUE_LEN)
                {
                    DebugPrintf("err len:%d\r\n",len);
                    return -1;
                }
                
#ifdef DEBUG
                int forloop=0;
                DebugPrintf("\r\n");
                for (forloop = 0; forloop < len; forloop++)
                {
                    DebugPrintf("%d ",data[forloop]);
                }
                DebugPrintf("\r\n");
#endif
                //data++;
                int first_fid=0;
                char tmp=data[0];
                data++;
                first_fid=((tmp<<8)|(data[0]));
                data++;
                DebugPrintf("ned csend:%d,%d,%d,%d\r\n",len,frame_numb,channel,first_fid);
                int i;   
                send_data_usb(data,len,channel,frame_numb,first_fid); 
                return 0;
}


int cal_crc16(char *data, int len)
{
    int crc = 0, i;
    char tmp;

    for ( i = 0; i < len; i++)
    {
        tmp = *data;
        data++;
        crc = ((crc << 8) & 0xFFFF) ^ (*(crc16_table + ((crc >> 8) ^ tmp)));
    }

    return crc;
}
void build_crc16_table()
{
    unsigned int i, j, nData;
    unsigned long crc;

    for (i = 0; i < 256; i++)
    {
        nData = i << 8;
        crc = 0;
        for (j = 0; j < 8; j++)
        {
            if ((nData ^ crc) & 0x8000)
            {
                crc = (crc << 1) ^ 0x8005;
            }
            else
            {
                crc <<= 1;
            }
            nData <<= 1;
        }

        *(crc16_table + i) = crc & 0xFFFF;
    }
}
